package com.ruoyi.nongjt.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.nongjt.domain.NjtUserPerfect;
import com.ruoyi.nongjt.domain.NjtUserRegister;
import com.ruoyi.nongjt.mapper.NjtUserPerfectMapper;
import com.ruoyi.nongjt.mapper.NjtUserRegisterMapper;
import com.ruoyi.nongjt.service.INjtUserPerfectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 用户资料完善Service业务层处理
 *
 * @author zhouyihao
 */
@Service
public class NjtUserPerfectServiceImpl implements INjtUserPerfectService {
    private static final Logger log = LoggerFactory.getLogger(NjtUserPerfectServiceImpl.class);

    @Autowired
    private NjtUserPerfectMapper njtUserPerfectMapper;

    @Autowired
    private NjtUserRegisterMapper njtUserRegisterMapper;

    /**
     * 查询用户资料完善
     *
     * @param id 用户资料完善主键
     * @return 用户资料完善
     */
    @Override
    public NjtUserPerfect selectNjtUserPerfectById(Long id) {
        log.debug("查询用户资料完善信息：{}", id);
        return njtUserPerfectMapper.selectNjtUserPerfectById(id);
    }

    /**
     * 根据用户ID查询用户资料完善
     *
     * @param userId 用户ID
     * @return 用户资料完善
     */
    @Override
    public NjtUserPerfect selectNjtUserPerfectByUserId(Long userId) {
        log.debug("根据用户ID查询用户资料完善信息：{}", userId);
        return njtUserPerfectMapper.selectNjtUserPerfectByUserId(userId);
    }

    /**
     * 查询用户资料完善列表
     *
     * @param njtUserPerfect 用户资料完善
     * @return 用户资料完善集合
     */
    @Override
    public List<NjtUserPerfect> selectNjtUserPerfectList(NjtUserPerfect njtUserPerfect) {
        log.debug("查询用户资料完善列表");
        return njtUserPerfectMapper.selectNjtUserPerfectList(njtUserPerfect);
    }

    /**
     * 新增用户资料完善
     *
     * @param njtUserPerfect 用户资料完善
     * @return 结果
     */
    @Override
    public int insertNjtUserPerfect(NjtUserPerfect njtUserPerfect) {
        log.debug("新增用户资料完善信息");
        njtUserPerfect.setCreateTime(new Date());
        njtUserPerfect.setDelFlag("0");
        return njtUserPerfectMapper.insertNjtUserPerfect(njtUserPerfect);
    }

    /**
     * 修改用户资料完善
     *
     * @param njtUserPerfect 用户资料完善
     * @return 结果
     */
    @Override
    public int updateNjtUserPerfect(NjtUserPerfect njtUserPerfect) {
        log.debug("修改用户资料完善信息：{}", njtUserPerfect.getUserId());
        njtUserPerfect.setUpdateTime(new Date());
        return njtUserPerfectMapper.updateNjtUserPerfect(njtUserPerfect);
    }

    /**
     * 删除用户资料完善信息
     *
     * @param id 用户资料完善主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserPerfectById(Long id) {
        log.debug("删除用户资料完善信息：{}", id);
        return njtUserPerfectMapper.deleteNjtUserPerfectById(id);
    }

    /**
     * 批量删除用户资料完善信息
     *
     * @param ids 需要删除的用户资料完善主键集合
     * @return 结果
     */
    @Override
    public int deleteNjtUserPerfectByIds(Long[] ids) {
        log.debug("批量删除用户资料完善信息");
        return njtUserPerfectMapper.deleteNjtUserPerfectByIds(ids);
    }

    /**
     * 提交用户完善资料
     *
     * @param njtUserPerfect 用户完善资料
     * @return 结果
     */
    @Override
    @Transactional
    public int submitPerfectInfo(NjtUserPerfect njtUserPerfect) {
        log.debug("用户提交完善资料：{}", njtUserPerfect.getUserId());
        
        // 设置提交时间和状态
        njtUserPerfect.setCreateTime(new Date());
        njtUserPerfect.setUpdateTime(new Date());
        njtUserPerfect.setPerfectStatus("1"); // 1表示审核中
        njtUserPerfect.setDelFlag("0");
        
        // 获取当前登录用户
        String loginName = SecurityUtils.getUsername();
        njtUserPerfect.setCreateBy(loginName);
        njtUserPerfect.setUpdateBy(loginName);
        
        // 先查询是否已存在该用户的完善资料
        NjtUserPerfect existingPerfect = selectNjtUserPerfectByUserId(njtUserPerfect.getUserId());
        int result;
        if (existingPerfect != null) {
            // 存在则更新
            njtUserPerfect.setId(existingPerfect.getId());
            result = updateNjtUserPerfect(njtUserPerfect);
        } else {
            // 不存在则新增
            result = insertNjtUserPerfect(njtUserPerfect);
        }
        
        // 更新用户注册表中的完善标志
        if (result > 0) {
            NjtUserRegister userRegister = new NjtUserRegister();
            userRegister.setId(njtUserPerfect.getUserId());
            userRegister.setPerfectFlag("1"); // 1表示已完善
            njtUserRegisterMapper.updateNjtUserRegister(userRegister);
        }
        
        return result;
    }

    /**
     * 更新用户完善资料
     *
     * @param njtUserPerfect 用户完善资料
     * @return 结果
     */
    @Override
    @Transactional
    public int updatePerfectInfo(NjtUserPerfect njtUserPerfect) {
        log.debug("用户更新完善资料：{}", njtUserPerfect.getUserId());
        
        // 设置更新时间和状态
        njtUserPerfect.setUpdateTime(new Date());
        njtUserPerfect.setPerfectStatus("1"); // 重新提交后状态变为审核中
        
        // 获取当前登录用户
        String loginName = SecurityUtils.getUsername();
        njtUserPerfect.setUpdateBy(loginName);
        
        return njtUserPerfectMapper.updateNjtUserPerfect(njtUserPerfect);
    }

    /**
     * 获取用户完善资料状态
     *
     * @param userId 用户ID
     * @return 完善状态
     */
    @Override
    public String getPerfectInfoStatus(Long userId) {
        log.debug("获取用户完善资料状态：{}", userId);
        
        // 先查询用户注册信息的完善标志
        NjtUserRegister userRegister = njtUserRegisterMapper.selectNjtUserRegisterById(userId);
        if (userRegister == null || "0".equals(userRegister.getPerfectFlag())) {
            return "0"; // 0表示未完善
        }
        
        // 查询完善资料的状态
        NjtUserPerfect perfectInfo = selectNjtUserPerfectByUserId(userId);
        if (perfectInfo != null && perfectInfo.getPerfectStatus() != null) {
            return perfectInfo.getPerfectStatus();
        }
        
        return "0"; // 默认返回未完善
    }

    /**
     * 审核用户完善资料
     *
     * @param userId 用户ID
     * @param perfectStatus 审核状态
     * @param auditRemark 审核意见
     * @return 结果
     */
    @Override
    @Transactional
    public int auditPerfectInfo(Long userId, String perfectStatus, String auditRemark) {
        log.debug("审核用户完善资料：{}, 状态：{}", userId, perfectStatus);
        
        // 获取当前登录用户
        String loginName = SecurityUtils.getUsername();
        
        // 更新完善资料状态
        return njtUserPerfectMapper.updatePerfectStatus(userId, perfectStatus, auditRemark, loginName);
    }
    
    /**
     * 更新用户完善资料状态
     */
    @Override
    @Transactional
    public int updatePerfectStatus(NjtUserPerfect njtUserPerfect) {
        log.debug("更新用户完善资料状态：{}, 状态：{}", njtUserPerfect.getUserId(), njtUserPerfect.getPerfectStatus());
        
        // 获取当前登录用户
        String loginName = SecurityUtils.getUsername();
        
        // 更新完善资料状态
        return njtUserPerfectMapper.updatePerfectStatus(
            njtUserPerfect.getUserId(), 
            njtUserPerfect.getPerfectStatus(), 
            njtUserPerfect.getAuditRemark(), 
            loginName
        );
    }
}